package com.dinner.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dinner.model.Administrator;
import com.dinner.model.DinnerUser;
import com.dinner.model.Seller;
import com.dinner.service.SellerService;
import com.dinner.util.PageHelper;

public class SellerManifestationAction extends Action {

	private SellerService service = null;

	/**
	 * @param service
	 *            the service to set
	 */
	public void setService(SellerService service) {
		this.service = service;
	}

	/**
	 * @return the service
	 */
	public SellerService getService() {
		return service;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		List<Seller> list = null;
		/***********************************************************************
		 * 
		 * NEED TO GET THE IMFORATION LU SEND TO ME
		 * 
		 **********************************************************************/

		DinnerUser user = (DinnerUser) session.getAttribute("user");
		if (user == null || user.getClass() != Administrator.class) {
			request.setAttribute("message", "Limit Of Authority.");
			return mapping.findForward("LoginError");
		}

		/*
		 * attention identifier == 0 will return the list of sellers make
		 * application otherwise return the sellers have authority
		 */
		String identifier = (String) request.getParameter("sellerType");	
		boolean auth = !identifier.equals("0");
		
		String pageNum = request.getParameter("pageNum");
		int page = pageNum == null ? 1 : Integer.parseInt(pageNum);
		
		PageHelper p = new PageHelper();
		p.setCurrentPage(page);
		p.setAdditionPath("sellerManifestation.do?sellerType="+identifier+"&");
		
		list = service.getSellersByAuthenticationAndPage(auth, p);
		session.setAttribute("list_of_user", list);
		request.setAttribute("pageInfo", p);
		
		if (auth) {
			return mapping.findForward("AdminManage");
		} else {
			return mapping.findForward("AdminValidate");
		}
		
	}
}
